package renguanyu.app.tools.model;

import java.io.IOException;
import java.sql.SQLException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.dbutils.QueryRunner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import renguanyu.app.tools.util.DbUtil;
import renguanyu.app.tools.util.SeleniumUtil;
import renguanyu.app.tools.util.ThreadUtil;

public class TestUi {

	private static WebDriver webDriver;

	// 公共
	public static void openBrowser() {
		webDriver = SeleniumUtil.getWebDriver();
	}

	public static void closeBrowser() {
		if (webDriver != null) {
			webDriver.close();
			webDriver = null;
		}
	}

	public static void killProcess() {
		Process exec;
		try {
			exec = Runtime.getRuntime().exec("taskkill /F /im chromedriver.exe");
			exec.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void waiting() {
		ThreadUtil.sleep(2);
	}

	public static void switchWindow() {
		String windowHandle = webDriver.getWindowHandle();
		Set<String> windowHandles = webDriver.getWindowHandles();
		List<String> allWindows = new ArrayList<String>(windowHandles);
		int j=0;
		for (int i = 0; i < allWindows.size(); i++) {
		    if (!allWindows.get(i).equals(windowHandle)) {
		        j = i;
		    }
		}
		webDriver.switchTo().window(allWindows.get(j));
	}
	

	public static void flushWindow() {
		webDriver.navigate().refresh();
	}

	
	
	// 百度
	public static void openIndex() {
		webDriver.get("https://www.baidu.com/");
	}

	public static void inputCsdn() {
		webDriver.findElement(By.id("kw")).sendKeys("csdn");
	}

	public static void clickSearch() {
		webDriver.findElement(By.id("su")).click();
	}
	
	// 阳光电影
	public static void openYgdy() {
		webDriver.get("https://www.ygdy8.net/index.html");
	}
	// 欧美电影
	public static void clickOumei() {
		By by = By.xpath("//a[text()='欧美电影']");
		new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(ExpectedConditions.presenceOfElementLocated(by));
		
		webDriver.findElement(by).click();
		
		switchWindow();
	}
	public static void getOumeiHref() {
		System.out.println("getOumeiHref");
		By by = By.xpath("//b/a[2]");
		new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(ExpectedConditions.presenceOfElementLocated(by));
		
		List<WebElement> elements = webDriver.findElements(by);
		for (WebElement webElement : elements) {
			String text = webElement.getText();
			String href = webElement.getAttribute("href");
			System.out.println(text + "|" + href);
			
			String sql = "INSERT INTO ygdy_movie_oumei (name,url) VALUES(?,?);";
			DbUtil.update(sql, text,href);
			
		}
	}
	public static void getOumeiTotal() {
		String xpath = "//div[@class='co_content8']/div[@class='x']";
		WebElement element = webDriver.findElement(By.xpath(xpath));
		String text = element.getText();
		// System.out.println(text);
		int indexOfStart = text.indexOf("共");
		int indexOfEnd = text.indexOf("页");
		String substring = text.substring(indexOfStart+1, indexOfEnd);
		System.out.println(substring);
	}

	public static void clickNextPage() {
		System.out.println("clickNextPage");
		String xpath = "//div[@class='co_content8']/div[@class='x']/a[text()='下一页']";
		By by = By.xpath(xpath);
		new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(ExpectedConditions.presenceOfElementLocated(by));

		
		WebElement element = webDriver.findElement(by);
		String href = element.getAttribute("href");
		System.out.println(href);
		element.click();
		
		switchWindow();
		
	}

	public static boolean isLastPage() {
		String xpath = "//div[@class='co_content8']/div[@class='x']/a[text()='下一页']";
		try {
			WebElement element = webDriver.findElement(By.xpath(xpath));
			System.out.println(false);
			return false;
		} catch (Exception e) {
			System.out.println(true);
			return true;
		}
	}

	// 阳光电影-详情
	public static void openDetailPage(String url) {
		webDriver.get(url);
	}


}
